// import { login, logout, getInfo } from '@/api/user'
import { getToken, setToken, removeToken } from '@/utils/auth'
// import { resetRouter } from '@/router'

import { getuserInfo, login, getMoreInfo } from '@/api/user'

const state = {
  token: getToken() || null,

  userInfo: null
}
const mutations = {
  setToken(state, payload) {
    state.token = payload.token
  },
  setuserInfo(state, payload) {
    state.userInfo = payload
  }
}
const actions = {
  async loginActions(context, payload) {
    const token = await login(payload)

    setToken(token)

    context.commit('setToken', { token })
  },

  async getuserInfoActions(context) {
    const res = await getuserInfo()

    const res1 = await getMoreInfo(res.userId)

    context.commit('setuserInfo', { ...res, ...res1 })
  },

  logout(context) {
    // 删除vuex里的token
    context.commit('setToken', { token: null })
    // 删除本地token
    removeToken()
    // 删除用户信息
    context.commit('setuserInfo', null)
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
